package com.vanke.kotlin_mvp.data.remote

import android.os.Handler
import com.vanke.kotlin_mvp.data.Task
import com.vanke.kotlin_mvp.data.TaskDataSource

/**
 * @name Kotlin_MVP
 * @package name：com.vanke.kotlin_mvp.data.local
 * @class describe
 * @anthor shiyuchuan
 * @time 2017/8/18 17:03
 */
class TasksRemoteDataSource : TaskDataSource {

    private val SERVICE_LATENCY_IN_MILLS = 5000L

    private val TASKS_SERVICE_DATA = LinkedHashMap<String, Task>(2)

    init {
        addTask("1", "1")
        addTask("2", "2")
    }

    private fun addTask(title: String, desc: String) {
        val newTask = Task(title, desc)
        TASKS_SERVICE_DATA.put(newTask.id, newTask)
    }

    override fun getTasks(callback: TaskDataSource.LoadTaskCallback) {
        val tasks = TASKS_SERVICE_DATA.values.toList()
        Handler().postDelayed({
            callback.onTaskLoaded(tasks)
        }, SERVICE_LATENCY_IN_MILLS)
    }

    override fun getTask(taskId: String, callback: TaskDataSource.GetTaskCallback) {

        val task = TASKS_SERVICE_DATA[taskId]
        with(Handler()) {
            if (task != null) {
                postDelayed({ callback.onTaskLoaded(task) }, SERVICE_LATENCY_IN_MILLS)
            } else {
                postDelayed({ callback.onDataAvailable() }, SERVICE_LATENCY_IN_MILLS)
            }
        }
    }

    override fun saveTask(task: Task) {
        TASKS_SERVICE_DATA.put(task.id, task)
    }

    override fun completeTask(task: Task) {
        val completedTask = Task(task.title, task.description, task.id).apply {
            isCompleted = true
        }
        TASKS_SERVICE_DATA.put(task.id, completedTask)
    }

    override fun completeTask(taskId: String) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun activateTask(task: Task) {
        val activeTask = Task(task.title, task.description, task.id)
        TASKS_SERVICE_DATA.put(task.id, activeTask)
    }

    override fun activateTask(taskId: String) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun clearCompletedTasks() {
        with(TASKS_SERVICE_DATA.entries.iterator()) {
            while (hasNext()) {
                if (next().value.isCompleted) remove()
            }
        }
    }

    override fun refreshTasks() {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun deleteAllTasks() {
        TASKS_SERVICE_DATA.clear()
    }

    override fun deleteTask(taskId: String) {
        TASKS_SERVICE_DATA.remove(taskId)
    }

    companion object {
        private var needNewInstance = true
        private lateinit var INSTANCE: TasksRemoteDataSource
        fun getInstance(): TasksRemoteDataSource {
            if (needNewInstance) {
                INSTANCE = TasksRemoteDataSource()
                needNewInstance = false
            }
            return INSTANCE
        }

    }
}